home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / ImageMagick / magick / compress.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  74.8 KB  |  2,832 lines

  1. /*
  2. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3. %                                                                             %
  4. %                                                                             %
  5. %                                                                             %
  6. %           CCCC   OOO   M   M  PPPP   RRRR   EEEEE   SSSSS  SSSSS            %
  7. %          C      O   O  MM MM  P   P  R   R  E       SS     SS               %
  8. %          C      O   O  M M M  PPPP   RRRR   EEE      SSS    SSS             %
  9. %          C      O   O  M   M  P      R R    E          SS     SS            %
  10. %           CCCC   OOO   M   M  P      R  R   EEEEE   SSSSS  SSSSS            %
  11. %                                                                             %
  12. %                                                                             %
  13. %                  Image Compression/Decompression Coders                     %
  14. %                                                                             %
  15. %                                                                             %
  16. %                                                                             %
  17. %                           Software Design                                   %
  18. %                             John Cristy                                     %
  19. %                              May  1993                                      %
  20. %                                                                             %
  21. %                                                                             %
  22. %  Copyright 1994 E. I. du Pont de Nemours & Company                          %
  23. %                                                                             %
  24. %  Permission to use, copy, modify, distribute, and sell this software and    %
  25. %  its documentation for any purpose is hereby granted without fee,           %
  26. %  provided that the above Copyright notice appear in all copies and that     %
  27. %  both that Copyright notice and this permission notice appear in            %
  28. %  supporting documentation, and that the name of E. I. du Pont de Nemours    %
  29. %  & Company not be used in advertising or publicity pertaining to            %
  30. %  distribution of the software without specific, written prior               %
  31. %  permission.  E. I. du Pont de Nemours & Company makes no representations   %
  32. %  about the suitability of this software for any purpose.  It is provided    %
  33. %  "as is" without express or implied warranty.                               %
  34. %                                                                             %
  35. %  E. I. du Pont de Nemours & Company disclaims all warranties with regard    %
  36. %  to this software, including all implied warranties of merchantability      %
  37. %  and fitness, in no event shall E. I. du Pont de Nemours & Company be       %
  38. %  liable for any special, indirect or consequential damages or any           %
  39. %  damages whatsoever resulting from loss of use, data or profits, whether    %
  40. %  in an action of contract, negligence or other tortious action, arising     %
  41. %  out of or in connection with the use or performance of this software.      %
  42. %                                                                             %
  43. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  44. %
  45. %
  46. %
  47. */
  48.  
  49. /*
  50.   Include declarations.
  51. */
  52. #include "magick.h"
  53. #include "image.h"
  54. #include "utility.h"
  55.  
  56. /*
  57.   Define declarations.
  58. */
  59. #define LowerBound  0
  60. #define MaxContextStates  121
  61. #define MinimumIntervalD  (unsigned short) 0xf000  /* ~-0.75 */
  62. #define MinimumIntervalE  (unsigned short) 0x1000  /* ~0.75 */
  63. #define No  0
  64. #define UpperBound  2
  65. #define Yes  1
  66. /*
  67.   State classification.
  68. */
  69. #define ZeroState  0
  70. #define SmallPostitiveState  1
  71. #define SmallNegativeState  2
  72. #define LargePostitiveState  3
  73. #define LargeNegativeState  4
  74.  
  75. /*
  76.   Typedef declarations.
  77. */
  78. typedef struct HuffmanTable
  79. {
  80.   int
  81.     id,
  82.     code,
  83.     length,
  84.     count;
  85. } HuffmanTable;
  86.  
  87. typedef struct _ScanlinePacket
  88. {
  89.   unsigned char
  90.     pixel;
  91.  
  92.   int
  93.     state;
  94. } ScanlinePacket;
  95.  
  96. /*
  97.   Huffman coding declarations.
  98. */
  99. #define TWId  23
  100. #define MWId  24
  101. #define TBId  25
  102. #define MBId  26
  103. #define EXId  27
  104.  
  105. static HuffmanTable
  106.   MBTable[]=
  107.   {
  108.     { MBId, 0x0f, 10, 64 }, { MBId, 0xc8, 12, 128 },
  109.     { MBId, 0xc9, 12, 192 }, { MBId, 0x5b, 12, 256 },
  110.     { MBId, 0x33, 12, 320 }, { MBId, 0x34, 12, 384 },
  111.     { MBId, 0x35, 12, 448 }, { MBId, 0x6c, 13, 512 },
  112.     { MBId, 0x6d, 13, 576 }, { MBId, 0x4a, 13, 640 },
  113.     { MBId, 0x4b, 13, 704 }, { MBId, 0x4c, 13, 768 },
  114.     { MBId, 0x4d, 13, 832 }, { MBId, 0x72, 13, 896 },
  115.     { MBId, 0x73, 13, 960 }, { MBId, 0x74, 13, 1024 },
  116.     { MBId, 0x75, 13, 1088 }, { MBId, 0x76, 13, 1152 },
  117.     { MBId, 0x77, 13, 1216 }, { MBId, 0x52, 13, 1280 },
  118.     { MBId, 0x53, 13, 1344 }, { MBId, 0x54, 13, 1408 },
  119.     { MBId, 0x55, 13, 1472 }, { MBId, 0x5a, 13, 1536 },
  120.     { MBId, 0x5b, 13, 1600 }, { MBId, 0x64, 13, 1664 },
  121.     { MBId, 0x65, 13, 1728 }, { MBId, 0x00, 0, 0 }
  122.   };
  123.  
  124. static HuffmanTable
  125.   EXTable[]=
  126.   {
  127.     { EXId, 0x08, 11, 1792 }, { EXId, 0x0c, 11, 1856 },
  128.     { EXId, 0x0d, 11, 1920 }, { EXId, 0x12, 12, 1984 },
  129.     { EXId, 0x13, 12, 2048 }, { EXId, 0x14, 12, 2112 },
  130.     { EXId, 0x15, 12, 2176 }, { EXId, 0x16, 12, 2240 },
  131.     { EXId, 0x17, 12, 2304 }, { EXId, 0x1c, 12, 2368 },
  132.     { EXId, 0x1d, 12, 2432 }, { EXId, 0x1e, 12, 2496 },
  133.     { EXId, 0x1f, 12, 2560 }, { EXId, 0x00, 0, 0 }
  134.   };
  135.  
  136. static HuffmanTable
  137.   MWTable[]=
  138.   {
  139.     { MWId, 0x1b, 5, 64 }, { MWId, 0x12, 5, 128 },
  140.     { MWId, 0x17, 6, 192 }, { MWId, 0x37, 7, 256 },
  141.     { MWId, 0x36, 8, 320 }, { MWId, 0x37, 8, 384 },
  142.     { MWId, 0x64, 8, 448 }, { MWId, 0x65, 8, 512 },
  143.     { MWId, 0x68, 8, 576 }, { MWId, 0x67, 8, 640 },
  144.     { MWId, 0xcc, 9, 704 }, { MWId, 0xcd, 9, 768 },
  145.     { MWId, 0xd2, 9, 832 }, { MWId, 0xd3, 9, 896 },
  146.     { MWId, 0xd4, 9, 960 }, { MWId, 0xd5, 9, 1024 },
  147.     { MWId, 0xd6, 9, 1088 }, { MWId, 0xd7, 9, 1152 },
  148.     { MWId, 0xd8, 9, 1216 }, { MWId, 0xd9, 9, 1280 },
  149.     { MWId, 0xda, 9, 1344 }, { MWId, 0xdb, 9, 1408 },
  150.     { MWId, 0x98, 9, 1472 }, { MWId, 0x99, 9, 1536 },
  151.     { MWId, 0x9a, 9, 1600 }, { MWId, 0x18, 6, 1664 },
  152.     { MWId, 0x9b, 9, 1728 }, { MWId, 0x00, 0, 0 }
  153.   };
  154.  
  155. static HuffmanTable
  156.   TBTable[]=
  157.   {
  158.     { TBId, 0x37, 10, 0 }, { TBId, 0x02, 3, 1 }, { TBId, 0x03, 2, 2 },
  159.     { TBId, 0x02, 2, 3 }, { TBId, 0x03, 3, 4 }, { TBId, 0x03, 4, 5 },
  160.     { TBId, 0x02, 4, 6 }, { TBId, 0x03, 5, 7 }, { TBId, 0x05, 6, 8 },
  161.     { TBId, 0x04, 6, 9 }, { TBId, 0x04, 7, 10 }, { TBId, 0x05, 7, 11 },
  162.     { TBId, 0x07, 7, 12 }, { TBId, 0x04, 8, 13 }, { TBId, 0x07, 8, 14 },
  163.     { TBId, 0x18, 9, 15 }, { TBId, 0x17, 10, 16 }, { TBId, 0x18, 10, 17 },
  164.     { TBId, 0x08, 10, 18 }, { TBId, 0x67, 11, 19 }, { TBId, 0x68, 11, 20 },
  165.     { TBId, 0x6c, 11, 21 }, { TBId, 0x37, 11, 22 }, { TBId, 0x28, 11, 23 },
  166.     { TBId, 0x17, 11, 24 }, { TBId, 0x18, 11, 25 }, { TBId, 0xca, 12, 26 },
  167.     { TBId, 0xcb, 12, 27 }, { TBId, 0xcc, 12, 28 }, { TBId, 0xcd, 12, 29 },
  168.     { TBId, 0x68, 12, 30 }, { TBId, 0x69, 12, 31 }, { TBId, 0x6a, 12, 32 },
  169.     { TBId, 0x6b, 12, 33 }, { TBId, 0xd2, 12, 34 }, { TBId, 0xd3, 12, 35 },
  170.     { TBId, 0xd4, 12, 36 }, { TBId, 0xd5, 12, 37 }, { TBId, 0xd6, 12, 38 },
  171.     { TBId, 0xd7, 12, 39 }, { TBId, 0x6c, 12, 40 }, { TBId, 0x6d, 12, 41 },
  172.     { TBId, 0xda, 12, 42 }, { TBId, 0xdb, 12, 43 }, { TBId, 0x54, 12, 44 },
  173.     { TBId, 0x55, 12, 45 }, { TBId, 0x56, 12, 46 }, { TBId, 0x57, 12, 47 },
  174.     { TBId, 0x64, 12, 48 }, { TBId, 0x65, 12, 49 }, { TBId, 0x52, 12, 50 },
  175.     { TBId, 0x53, 12, 51 }, { TBId, 0x24, 12, 52 }, { TBId, 0x37, 12, 53 },
  176.     { TBId, 0x38, 12, 54 }, { TBId, 0x27, 12, 55 }, { TBId, 0x28, 12, 56 },
  177.     { TBId, 0x58, 12, 57 }, { TBId, 0x59, 12, 58 }, { TBId, 0x2b, 12, 59 },
  178.     { TBId, 0x2c, 12, 60 }, { TBId, 0x5a, 12, 61 }, { TBId, 0x66, 12, 62 },
  179.     { TBId, 0x67, 12, 63 }, { TBId, 0x00, 0, 0 }
  180.   };
  181.  
  182. static HuffmanTable
  183.   TWTable[]=
  184.   {
  185.     { TWId, 0x35, 8, 0 }, { TWId, 0x07, 6, 1 }, { TWId, 0x07, 4, 2 },
  186.     { TWId, 0x08, 4, 3 }, { TWId, 0x0b, 4, 4 }, { TWId, 0x0c, 4, 5 },
  187.     { TWId, 0x0e, 4, 6 }, { TWId, 0x0f, 4, 7 }, { TWId, 0x13, 5, 8 },
  188.     { TWId, 0x14, 5, 9 }, { TWId, 0x07, 5, 10 }, { TWId, 0x08, 5, 11 },
  189.     { TWId, 0x08, 6, 12 }, { TWId, 0x03, 6, 13 }, { TWId, 0x34, 6, 14 },
  190.     { TWId, 0x35, 6, 15 }, { TWId, 0x2a, 6, 16 }, { TWId, 0x2b, 6, 17 },
  191.     { TWId, 0x27, 7, 18 }, { TWId, 0x0c, 7, 19 }, { TWId, 0x08, 7, 20 },
  192.     { TWId, 0x17, 7, 21 }, { TWId, 0x03, 7, 22 }, { TWId, 0x04, 7, 23 },
  193.     { TWId, 0x28, 7, 24 }, { TWId, 0x2b, 7, 25 }, { TWId, 0x13, 7, 26 },
  194.     { TWId, 0x24, 7, 27 }, { TWId, 0x18, 7, 28 }, { TWId, 0x02, 8, 29 },
  195.     { TWId, 0x03, 8, 30 }, { TWId, 0x1a, 8, 31 }, { TWId, 0x1b, 8, 32 },
  196.     { TWId, 0x12, 8, 33 }, { TWId, 0x13, 8, 34 }, { TWId, 0x14, 8, 35 },
  197.     { TWId, 0x15, 8, 36 }, { TWId, 0x16, 8, 37 }, { TWId, 0x17, 8, 38 },
  198.     { TWId, 0x28, 8, 39 }, { TWId, 0x29, 8, 40 }, { TWId, 0x2a, 8, 41 },
  199.     { TWId, 0x2b, 8, 42 }, { TWId, 0x2c, 8, 43 }, { TWId, 0x2d, 8, 44 },
  200.     { TWId, 0x04, 8, 45 }, { TWId, 0x05, 8, 46 }, { TWId, 0x0a, 8, 47 },
  201.     { TWId, 0x0b, 8, 48 }, { TWId, 0x52, 8, 49 }, { TWId, 0x53, 8, 50 },
  202.     { TWId, 0x54, 8, 51 }, { TWId, 0x55, 8, 52 }, { TWId, 0x24, 8, 53 },
  203.     { TWId, 0x25, 8, 54 }, { TWId, 0x58, 8, 55 }, { TWId, 0x59, 8, 56 },
  204.     { TWId, 0x5a, 8, 57 }, { TWId, 0x5b, 8, 58 }, { TWId, 0x4a, 8, 59 },
  205.     { TWId, 0x4b, 8, 60 }, { TWId, 0x32, 8, 61 }, { TWId, 0x33, 8, 62 },
  206.     { TWId, 0x34, 8, 63 }, { TWId, 0x00, 0, 0 }
  207.   };
  208.  
  209. /*
  210.   Static Q-coder declarations.
  211. */
  212. static int
  213.   decrement_less_probable[]=
  214.   {
  215.     0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  216.     2, 2, 3, 2, 3, 2, 3, 2
  217.   };
  218.  
  219. static int
  220.   increment_more_probable[]=
  221.   {
  222.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  223.     1, 1, 1, 1, 1, 1, 1, 0
  224.   };
  225.  
  226. static int
  227.   more_probable_exchange[]=
  228.   {
  229.     1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  230.     0, 0, 0, 0, 0, 0, 0, 0
  231.   };
  232.  
  233. static int
  234.   statistics[][5]=
  235.   {
  236.      0,  4,  8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64,
  237.     68, 72, 76, 80, 84, 88, 92, 96
  238.   };
  239.  
  240. static unsigned short
  241.   probability[]=
  242.   {
  243.     0x0ac1, 0x0a81, 0x0a01, 0x0901, 0x0701, 0x0681,
  244.     0x0601, 0x0501, 0x0481, 0x0441, 0x0381, 0x0301,
  245.     0x02c1, 0x0281, 0x0241, 0x0181, 0x0121, 0x00e1,
  246.     0x00a1, 0x0071, 0x0059, 0x0053, 0x0027, 0x0017,
  247.     0x0013, 0x000b, 0x0007, 0x0005, 0x0003, 0x0001
  248.   };
  249. /*
  250.   Declarations and initializations for predictive arithimetic coder.
  251. */
  252. static int
  253.   code,
  254.   less_probable[MaxContextStates],
  255.   more_probable[MaxContextStates],
  256.   probability_estimate[MaxContextStates];
  257.  
  258. static unsigned char
  259.   *q;
  260.  
  261. static unsigned short
  262.   interval;
  263.  
  264. /*
  265. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  266. %                                                                             %
  267. %                                                                             %
  268. %                                                                             %
  269. %   B M P D e c o d e I m a g e                                               %
  270. %                                                                             %
  271. %                                                                             %
  272. %                                                                             %
  273. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  274. %
  275. %  Function BMPDecodeImage unpacks the packed image pixels into
  276. %  runlength-encoded pixel packets.
  277. %
  278. %  The format of the BMPDecodeImage routine is:
  279. %
  280. %      status=BMPDecodeImage(compressed_pixels,pixels,compression,
  281. %        number_columns,number_rows)
  282. %
  283. %  A description of each parameter follows:
  284. %
  285. %    o status:  Function BMPDecodeImage returns True if all the pixels are
  286. %      uncompressed without error, otherwise False.
  287. %
  288. %    o compressed_pixels:  The address of a byte (8 bits) array of compressed
  289. %      pixel data.
  290. %
  291. %    o pixels:  The address of a byte (8 bits) array of pixel data created by
  292. %      the decoding process.
  293. %
  294. %    o compression:  A value of 1 means the compressed pixels are runlength
  295. %      encoded for a 256-color bitmap.  A value of 2 means a 16-color bitmap.
  296. %
  297. %    o number_columns:  An integer value that is the number of columns or
  298. %      width in pixels of your source image.
  299. %
  300. %    o number_rows:  An integer value that is the number of rows or
  301. %      heigth in pixels of your source image.
  302. %
  303. %
  304. */
  305. unsigned int BMPDecodeImage(compressed_pixels,pixels,compression,
  306.   number_columns,number_rows)
  307. unsigned char
  308.   *compressed_pixels,
  309.   *pixels;
  310.  
  311. unsigned int
  312.   compression,
  313.   number_columns,
  314.   number_rows;
  315. {
  316.   register int
  317.     i,
  318.     x,
  319.     y;
  320.  
  321.   register unsigned char
  322.     *p,
  323.     *q;
  324.  
  325.   unsigned char
  326.     byte,
  327.     count;
  328.  
  329.   p=compressed_pixels;
  330.   q=pixels;
  331.   x=0;
  332.   for (y=0; y < number_rows; )
  333.   {
  334.     count=(*p++);
  335.     if (count != 0)
  336.       {
  337.         /*
  338.           Encoded mode.
  339.         */
  340.         byte=(*p++);
  341.         for (i=0; i < (int) count; i++)
  342.         {
  343.           if (compression == 1)
  344.             *q++=byte;
  345.           else
  346.             *q++=(i & 0x01) ? (byte & 0x0f) : ((byte >> 4) & 0x0f);
  347.           x++;
  348.         }
  349.       }
  350.     else
  351.       {
  352.         /*
  353.           Escape mode.
  354.         */
  355.         count=(*p++);
  356.         if (count == 0x01)
  357.           return(True);
  358.         switch (count)
  359.         {
  360.           case 0x00:
  361.           {
  362.             /*
  363.               End of line.
  364.             */
  365.             x=0;
  366.             y++;
  367.             q=pixels+y*number_columns;
  368.             break;
  369.           }
  370.           case 0x02:
  371.           {
  372.             /*
  373.               Delta mode.
  374.             */
  375.             x+=(*p++);
  376.             y+=(*p++);
  377.             q=pixels+y*number_columns+x;
  378.             break;
  379.           }
  380.           default:
  381.           {
  382.             /*
  383.               Absolute mode.
  384.             */
  385.             for (i=0; i < (int) count; i++)
  386.             {
  387.               if (compression == 1)
  388.                 *q++=(*p++);
  389.               else
  390.                 {
  391.                   if ((i & 0x01) == 0)
  392.                     byte=(*p++);
  393.                   *q++=(i & 0x01) ? (byte & 0x0f) : ((byte >> 4) & 0x0f);
  394.                 }
  395.               x++;
  396.             }
  397.             /*
  398.               Read pad byte.
  399.             */
  400.             if (compression == 1)
  401.               {
  402.                 if (count & 0x01)
  403.                   p++;
  404.               }
  405.             else
  406.               if (((count & 0x03) == 1) || ((count & 0x03) == 2))
  407.                 p++;
  408.             break;
  409.           }
  410.         }
  411.      }
  412.   }
  413.   return(False);
  414. }
  415.  
  416. /*
  417. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  418. %                                                                             %
  419. %                                                                             %
  420. %                                                                             %
  421. %   B M P E n c o d e I m a g e                                               %
  422. %                                                                             %
  423. %                                                                             %
  424. %                                                                             %
  425. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  426. %
  427. %  Function BMPEncodeImage compresses pixels using a runlength encoded format.
  428. %
  429. %  The format of the BMPEncodeImage routine is:
  430. %
  431. %      status=BMPEncodeImage(pixels,compressed_pixels,number_columns,
  432. %        number_rows)
  433. %
  434. %  A description of each parameter follows:
  435. %
  436. %    o status:  Function BMPEncodeImage returns the number of bytes in the
  437. %      runlength encoded compress_pixels array.
  438. %
  439. %    o pixels:  The address of a byte (8 bits) array of pixel data created by
  440. %      the compression process.
  441. %
  442. %    o compressed_pixels:  The address of a byte (8 bits) array of compressed
  443. %      pixel data.
  444. %
  445. %    o number_columns:  An integer value that is the number of columns or
  446. %      width in pixels of your source image.
  447. %
  448. %    o number_rows:  An integer value that is the number of rows or
  449. %      heigth in pixels of your source image.
  450. %
  451. %
  452. */
  453. unsigned int BMPEncodeImage(pixels,compressed_pixels,number_columns,number_rows)
  454. unsigned char
  455.   *pixels,
  456.   *compressed_pixels;
  457.  
  458. unsigned int
  459.   number_columns,
  460.   number_rows;
  461. {
  462.   register int
  463.     i,
  464.     x,
  465.     y;
  466.  
  467.   register unsigned char
  468.     *p,
  469.     *q;
  470.  
  471.   /*
  472.     Runlength encode pixels.
  473.   */
  474.   p=pixels;
  475.   q=compressed_pixels;
  476.   for (y=0; y < number_rows; y++)
  477.   {
  478.     for (x=0; x < number_columns; x+=i)
  479.     {
  480.       /*
  481.         Determine runlength.
  482.       */
  483.       for (i=1; ((x+i) < number_columns); i++)
  484.         if ((*(p+i) != *p) || (i == 255))
  485.           break;
  486.       *q++=i;
  487.       *q++=(*p);
  488.       p+=i;
  489.     }
  490.     /*
  491.       End of line.
  492.     */
  493.     *q++=0;
  494.     *q++=0x00;
  495.   }
  496.   /*
  497.     End of bitmap.
  498.   */
  499.   *q++=0;
  500.   *q++=0x01;
  501.   return(q-compressed_pixels);
  502. }
  503.  
  504. /*
  505. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  506. %                                                                             %
  507. %                                                                             %
  508. %                                                                             %
  509. %   D e c o d e                                                               %
  510. %                                                                             %
  511. %                                                                             %
  512. %                                                                             %
  513. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  514. %
  515. %  Function Decode uncompresses a string.
  516. %
  517. %  The format of the Decode routine is:
  518. %
  519. %      Decode(state,decision)
  520. %
  521. %  A description of each parameter follows:
  522. %
  523. %    o state:  An integer value representing the current state.
  524. %
  525. %    o decision:  A pointer to an integer.  The output of the binary
  526. %      decision (Yes/No) is returned in this value.
  527. %
  528. %
  529. */
  530. static void Decode(state,decision)
  531. register int
  532.   state,
  533.   *decision;
  534. {
  535.   interval+=probability[probability_estimate[state]];
  536.   if (((code >> 16) & 0xffff) < ((int) interval))
  537.     {
  538.       code-=(interval << 16);
  539.       interval=(-probability[probability_estimate[state]]);
  540.       *decision=less_probable[state];
  541.     }
  542.   else
  543.     {
  544.       *decision=more_probable[state];
  545.       if (interval <= MinimumIntervalD)
  546.         return;
  547.     }
  548.   do
  549.   {
  550.     if ((code & 0xff) == 0)
  551.       {
  552.         code&=0xffff0000;
  553.         if ((*q++) == 0xff)
  554.           code+=((int) (*q) << 9)+0x02;
  555.         else
  556.           code+=((int) (*q) << 8)+0x01;
  557.       }
  558.     interval<<=1;
  559.     code<<=1;
  560.   } while (interval > MinimumIntervalD);
  561.   /*
  562.     Update probability estimates.
  563.   */
  564.   if (*decision == more_probable[state])
  565.     probability_estimate[state]+=
  566.       increment_more_probable[probability_estimate[state]];
  567.   else
  568.     probability_estimate[state]-=
  569.       decrement_less_probable[probability_estimate[state]];
  570.   if (more_probable_exchange[probability_estimate[state]] != 0)
  571.     {
  572.       /*
  573.         Exchange sense of most probable and least probable.
  574.       */
  575.       less_probable[state]=more_probable[state];
  576.       more_probable[state]=1-more_probable[state];
  577.     }
  578. }
  579.  
  580. /*
  581. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  582. %                                                                             %
  583. %                                                                             %
  584. %                                                                             %
  585. %   E n c o d e                                                               %
  586. %                                                                             %
  587. %                                                                             %
  588. %                                                                             %
  589. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  590. %
  591. %  Function Encode generate compressed data string by encoding yes-no decision
  592. %  given state s.
  593. %
  594. %  The format of the Encode routine is:
  595. %
  596. %      Encode(state,decision)
  597. %
  598. %  A description of each parameter follows:
  599. %
  600. %    o state:  An integer value representing the current state.
  601. %
  602. %    o decision:  An integer value representing a binary decision.
  603. %
  604. %
  605. */
  606. static void Encode(state,decision)
  607. register int
  608.   state,
  609.   decision;
  610. {
  611.   /*
  612.     Test on "most-probable-symbol" for state s(more_probable[state])
  613.   */
  614.   interval-=probability[probability_estimate[state]];
  615.   if (more_probable[state] != decision)
  616.     {
  617.       code-=interval;
  618.       interval=probability[probability_estimate[state]];
  619.     }
  620.   else
  621.     if (interval >= MinimumIntervalE)
  622.       return;
  623.   /*
  624.     Encoder renormalization.
  625.   */
  626.   do
  627.   {
  628.     interval<<=1;
  629.     if (code >= 0)
  630.       code<<=1;
  631.     else
  632.       {
  633.         /*
  634.           Shift unsigned char of data from Code register to compressed string.
  635.         */
  636.         code<<=1;
  637.         if (code > 0)
  638.           {
  639.             /*
  640.               Add eight bits from Code register to compressed data string.
  641.             */
  642.             (*q++)--;
  643.             *q=(unsigned char) (code >> 16);
  644.             code&=0x0000ffff;
  645.             code|=0x01800000;
  646.           }
  647.         else
  648.           {
  649.             code&=0x01ffffff;
  650.             if ((int) interval > code)
  651.               {
  652.                 /*
  653.                   Add eight bits from Code register to compressed data string.
  654.                 */
  655.                 (*q++)--;
  656.                 *q=0xff;
  657.                 code|=0x01810000;
  658.               }
  659.             else
  660.               if ((*q++) == 0xff)
  661.                 {
  662.                   /*
  663.                     Add seven bits from Code register plus one stuffed bit to
  664.                     compressed data string.
  665.                   */
  666.                   *q=(unsigned char) (code >> 17);
  667.                   code&=0x0001ffff;
  668.                   code|=0x03000000;
  669.                 }
  670.               else
  671.                 {
  672.                   /*
  673.                     Add eight bits from Code register to compressed data string.
  674.                   */
  675.                   *q=(unsigned char) (code >> 16);
  676.                   code&=0x0000ffff;
  677.                   code|=0x01800000;
  678.                 }
  679.           }
  680.       }
  681.   } while (interval < MinimumIntervalE);
  682.   /*
  683.     Update probability estimates
  684.   */
  685.   if (decision == more_probable[state])
  686.     probability_estimate[state]+=
  687.       increment_more_probable[probability_estimate[state]];
  688.   else
  689.     probability_estimate[state]-=
  690.       decrement_less_probable[probability_estimate[state]];
  691.   if (more_probable_exchange[probability_estimate[state]] != 0)
  692.     more_probable[state]=1-more_probable[state];
  693. }
  694.  
  695. /*
  696. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  697. %                                                                             %
  698. %                                                                             %
  699. %                                                                             %
  700. %   F l u s h                                                                 %
  701. %                                                                             %
  702. %                                                                             %
  703. %                                                                             %
  704. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  705. %
  706. %  Function Flush flushes the final bits of data from the Code register to the
  707. %  compressed data string.
  708. %
  709. %  The format of the Flush routine is:
  710. %
  711. %      Flush()
  712. %
  713. %
  714. */
  715. static void Flush()
  716. {
  717.   register int
  718.     extra_bits;
  719.  
  720.   code-=interval;
  721.   extra_bits=24;
  722.   extra_bits--;
  723.   while (code >= 0)
  724.   {
  725.     code<<=1;
  726.     extra_bits--;
  727.   }
  728.   code<<=1;
  729.   if (code > 0)
  730.     (*q)--;
  731.   /*
  732.     Add the final compressed data unsigned chars to the compressed data string.
  733.   */
  734.   do
  735.   {
  736.     if ((*q++) == 0xff)
  737.       {
  738.         /*
  739.           Add seven bits of data plus one stuffed bit to the compressed data
  740.           string during final Flush of Code register.
  741.         */
  742.         *q=(unsigned char) (code >> 17);
  743.         code&=0x0001ffff;
  744.         code<<=7;
  745.         extra_bits-=7;
  746.       }
  747.     else
  748.       {
  749.         /*
  750.            Add eight bits of data to the compressed data string during final
  751.            flush of Code register.
  752.         */
  753.         *q=(unsigned char) (code >> 16);
  754.         code&=0x0000ffff;
  755.         code<<=8;
  756.         extra_bits-=8;
  757.       }
  758.   } while (extra_bits > 0);
  759.   q++;
  760. }
  761.  
  762. /*
  763. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  764. %                                                                             %
  765. %                                                                             %
  766. %                                                                             %
  767. %   H u f f m a n D e c o d e I m a g e                                       %
  768. %                                                                             %
  769. %                                                                             %
  770. %                                                                             %
  771. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  772. %
  773. %  Function HuffmanDecodeImage uncompresses an image via Huffman-coding.
  774. %
  775. %  The format of the HuffmanDecodeImage routine is:
  776. %
  777. %      status=HuffmanDecodeImage(image)
  778. %
  779. %  A description of each parameter follows:
  780. %
  781. %    o status:  Function HuffmanDecodeImage returns True if all the pixels are
  782. %      compressed without error, otherwise False.
  783. %
  784. %    o image: The address of a structure of type Image.
  785. %
  786. %
  787. */
  788. unsigned int HuffmanDecodeImage(image)
  789. Image
  790.   *image;
  791. {
  792. #define HashSize  1021
  793. #define MBHashA  293
  794. #define MBHashB  2695
  795. #define MWHashA  3510
  796. #define MWHashB  1178
  797.  
  798. #define InitializeHashTable(hash,table,a,b) \
  799. {  \
  800.   entry=table;  \
  801.   while (entry->code != 0)  \
  802.   {  \
  803.     hash[((entry->length+a)*(entry->code+b)) % HashSize]=entry;  \
  804.     entry++;  \
  805.   }  \
  806. }
  807.  
  808. #define InputBit(bit)  \
  809. {  \
  810.   if ((mask & 0xff) == 0)  \
  811.     {  \
  812.       byte=getc(image->file);  \
  813.       mask=0x80;  \
  814.     }  \
  815.   runlength++;  \
  816.   bit=byte & mask ? 1 : 0; \
  817.   mask>>=1;  \
  818.   if (bit)  \
  819.     runlength=0;  \
  820. }
  821.  
  822.   HuffmanTable
  823.     *entry,
  824.     **mb_hash,
  825.     **mw_hash;
  826.  
  827.   int
  828.     code,
  829.     color,
  830.     count,
  831.     length,
  832.     null_lines,
  833.     runlength,
  834.     x,
  835.     y;
  836.  
  837.   register int
  838.     i;
  839.  
  840.   register RunlengthPacket
  841.     *q;
  842.  
  843.   register unsigned char
  844.     *p;
  845.  
  846.   unsigned char
  847.     bit,
  848.     byte,
  849.     mask,
  850.     *scanline;
  851.  
  852.   /*
  853.     Allocate buffers.
  854.   */
  855.   mb_hash=(HuffmanTable **) malloc(HashSize*sizeof(HuffmanTable *));
  856.   mw_hash=(HuffmanTable **) malloc(HashSize*sizeof(HuffmanTable *));
  857.   scanline=(unsigned char *) malloc(image->columns*sizeof(unsigned char));
  858.   if ((mb_hash == (HuffmanTable **) NULL) ||
  859.       (mw_hash == (HuffmanTable **) NULL) ||
  860.       (scanline == (unsigned char *) NULL))
  861.     {
  862.       Warning("Unable to allocate memory",(char *) NULL);
  863.       return(False);
  864.     }
  865.   /*
  866.     Initialize Huffman tables.
  867.   */
  868.   for (i=0; i < HashSize; i++)
  869.   {
  870.     mb_hash[i]=(HuffmanTable *) NULL;
  871.     mw_hash[i]=(HuffmanTable *) NULL;
  872.   }
  873.   InitializeHashTable(mw_hash,TWTable,MWHashA,MWHashB);
  874.   InitializeHashTable(mw_hash,MWTable,MWHashA,MWHashB);
  875.   InitializeHashTable(mw_hash,EXTable,MWHashA,MWHashB);
  876.   InitializeHashTable(mb_hash,TBTable,MBHashA,MBHashB);
  877.   InitializeHashTable(mb_hash,MBTable,MBHashA,MBHashB);
  878.   InitializeHashTable(mb_hash,EXTable,MBHashA,MBHashB);
  879.   /*
  880.     Uncompress 1D Huffman to runlength encoded pixels.
  881.   */
  882.   mask=0;
  883.   null_lines=0;
  884.   q=image->pixels;
  885.   for (y=0; ((y < image->rows) && (null_lines < 3)); y++)
  886.   {
  887.     /*
  888.       Initialize scanline to white.
  889.     */
  890.     p=scanline;
  891.     for (x=0; x < image->columns; x++)
  892.       *p++=0;
  893.     /*
  894.       Decode Huffman encoded scanline.
  895.     */
  896.     color=True;
  897.     code=0;
  898.     count=0;
  899.     length=0;
  900.     runlength=0;
  901.     for (x=0; x < image->columns; )
  902.     {
  903.       do
  904.       {
  905.         if (runlength < 11)
  906.           InputBit(bit)
  907.         else
  908.           {
  909.             InputBit(bit);
  910.             if (bit)
  911.               length=13;
  912.           }
  913.         code=(code << 1)+bit;
  914.         length++;
  915.       } while ((code <= 0) && (length < 14));
  916.       if (length > 13)
  917.         break;
  918.       if (color)
  919.         {
  920.           if (length < 4)
  921.             continue;
  922.           entry=mw_hash[((length+MWHashA)*(code+MWHashB)) % HashSize];
  923.         }
  924.       else
  925.         {
  926.           if (length < 2)
  927.             continue;
  928.           entry=mb_hash[((length+MBHashA)*(code+MBHashB)) % HashSize];
  929.         }
  930.       if (!entry)
  931.         continue;
  932.       else
  933.         if ((entry->length != length) || (entry->code != code))
  934.           continue;
  935.       switch (entry->id)
  936.       {
  937.         case TWId:
  938.         case TBId:
  939.         {
  940.           count+=entry->count;
  941.           if ((x+count) > image->columns)
  942.             count=image->columns-x;
  943.           if (count > 0)
  944.             if (color)
  945.               {
  946.                 x+=count;
  947.                 count=0;
  948.               }
  949.             else
  950.               for ( ; count > 0; count--)
  951.                 scanline[x++]=1;
  952.           color=!color;
  953.           break;
  954.         }
  955.         case MWId:
  956.         case MBId:
  957.         {
  958.           count+=entry->count;
  959.           break;
  960.         }
  961.         case EXId:
  962.         {
  963.           count+=entry->count;
  964.           break;
  965.         }
  966.         default:
  967.           break;
  968.       }
  969.       code=0;
  970.       length=0;
  971.     }
  972.     null_lines++;
  973.     if (x != 0)
  974.       {
  975.         /*
  976.           Skip to end of scanline.
  977.         */
  978.         while (runlength < 11)
  979.           InputBit(bit);
  980.         do
  981.         {
  982.           InputBit(bit);
  983.         } while (bit == 0);
  984.         null_lines=0;
  985.       }
  986.     /*
  987.       Transfer scanline to image pixels.
  988.     */
  989.     p=scanline;
  990.     for (x=0; x < image->columns; x++)
  991.     {
  992.       q->index=(unsigned short) (*p);
  993.       q->length=0;
  994.       p++;
  995.       q++;
  996.     }
  997.   }
  998.   image->rows=y-1;
  999.   /*
  1000.     Free decoder memory.
  1001.   */
  1002.   (void) free((char *) mw_hash);
  1003.   (void) free((char *) mb_hash);
  1004.   (void) free((char *) scanline);
  1005.   return(True);
  1006. }
  1007.  
  1008. /*
  1009. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1010. %                                                                             %
  1011. %                                                                             %
  1012. %                                                                             %
  1013. %   H u f f m a n E n c o d e I m a g e                                       %
  1014. %                                                                             %
  1015. %                                                                             %
  1016. %                                                                             %
  1017. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1018. %
  1019. %  Function HuffmanEncodeImage compresses an image via Huffman-coding.
  1020. %
  1021. %  The format of the HuffmanEncodeImage routine is:
  1022. %
  1023. %      status=HuffmanEncodeImage(image)
  1024. %
  1025. %  A description of each parameter follows:
  1026. %
  1027. %    o status:  Function HuffmanEncodeImage returns True if all the pixels are
  1028. %      compressed without error, otherwise False.
  1029. %
  1030. %    o image: The address of a structure of type Image.
  1031. %
  1032. %
  1033. */
  1034. unsigned int HuffmanEncodeImage(image)
  1035. Image
  1036.   *image;
  1037. {
  1038. #define HuffmanOutputCode(entry)  \
  1039. {  \
  1040.   mask=1 << (entry->length-1);  \
  1041.   while (mask != 0)  \
  1042.   {  \
  1043.     OutputBit((entry->code & mask ? 1 : 0));  \
  1044.     mask>>=1;  \
  1045.   }  \
  1046. }
  1047.  
  1048. #define OutputBit(count)  \
  1049. {  \
  1050.   if(count > 0)  \
  1051.     byte=byte | bit;  \
  1052.   bit>>=1;  \
  1053.   if ((bit & 0xff) == 0)   \
  1054.     {  \
  1055.       (void) fputc((char) byte,image->file);  \
  1056.       byte=0;  \
  1057.       bit=0x80;  \
  1058.     }  \
  1059. }
  1060.  
  1061.   HuffmanTable
  1062.     *entry;
  1063.  
  1064.   int
  1065.     i,
  1066.     k,
  1067.     runlength;
  1068.  
  1069.   register int
  1070.     j,
  1071.     n,
  1072.     x;
  1073.  
  1074.   register RunlengthPacket
  1075.     *p;
  1076.  
  1077.   register unsigned char
  1078.     *q;
  1079.  
  1080.   register unsigned short
  1081.     polarity;
  1082.  
  1083.   unsigned char
  1084.     bit,
  1085.     byte,
  1086.     *scanline;
  1087.  
  1088.   unsigned int
  1089.     mask;
  1090.  
  1091.   /*
  1092.     Allocate scanline buffer.
  1093.   */
  1094.   scanline=(unsigned char *)
  1095.     malloc((Max(image->columns,1728)+1)*sizeof(unsigned char));
  1096.   if (scanline == (unsigned char *) NULL)
  1097.     {
  1098.       Warning("Unable to allocate memory",(char *) NULL);
  1099.       return(False);
  1100.     }
  1101.   /*
  1102.     Compress runlength encoded to 1D Huffman pixels.
  1103.   */
  1104.   polarity=0;
  1105.   if (image->colors == 2)
  1106.     polarity=(Intensity(image->colormap[0]) >
  1107.       Intensity(image->colormap[1]) ? 0 : 1);
  1108.   q=scanline;
  1109.   for (i=0; i < Max(image->columns,1728); i++)
  1110.     *q++=polarity;
  1111.   byte=0;
  1112.   bit=0x80;
  1113.   p=image->pixels;
  1114.   q=scanline;
  1115.   x=0;
  1116.   for (i=0; i < image->packets; i++)
  1117.   {
  1118.     for (j=0; j <= ((int) p->length); j++)
  1119.     {
  1120.       *q++=(unsigned char) (p->index == polarity ? polarity : !polarity);
  1121.       x++;
  1122.       if (x < image->columns)
  1123.         continue;
  1124.       /*
  1125.         Huffman encode scanline.
  1126.       */
  1127.       q=scanline;
  1128.       for (n=Max(image->columns,1728); n > 0; )
  1129.       {
  1130.         /*
  1131.           Output white run.
  1132.         */
  1133.         for (runlength=0; ((*q == polarity) && (n > 0)); n--)
  1134.         {
  1135.           q++;
  1136.           runlength++;
  1137.         }
  1138.         if (runlength >= 64)
  1139.           {
  1140.             entry=MWTable+((runlength/64)-1);
  1141.             runlength-=entry->count;
  1142.             HuffmanOutputCode(entry);
  1143.           }
  1144.         entry=TWTable+runlength;
  1145.         HuffmanOutputCode(entry);
  1146.         if (n != 0)
  1147.           {
  1148.             /*
  1149.               Output black run.
  1150.             */
  1151.             for (runlength=0; ((*q != polarity) && (n > 0)); n--)
  1152.             {
  1153.               q++;
  1154.               runlength++;
  1155.             }
  1156.             if (runlength >= 64)
  1157.               {
  1158.                 entry=MBTable+((runlength/64)-1);
  1159.                 runlength-=entry->count;
  1160.                 HuffmanOutputCode(entry);
  1161.               }
  1162.             entry=TBTable+runlength;
  1163.             HuffmanOutputCode(entry);
  1164.           }
  1165.       }
  1166.       /*
  1167.         End of line.
  1168.       */
  1169.       for (k=0; k < 11; k++)
  1170.         OutputBit(0);
  1171.       OutputBit(1);
  1172.       x=0;
  1173.       q=scanline;
  1174.     }
  1175.     p++;
  1176.   }
  1177.   /*
  1178.     End of page.
  1179.   */
  1180.   for (i=0; i < 6; i++)
  1181.   {
  1182.     for (k=0; k < 11; k++)
  1183.       OutputBit(0);
  1184.     OutputBit(1);
  1185.   }
  1186.   /*
  1187.     Flush bits.
  1188.   */
  1189.   if (bit != 0x80)
  1190.     (void) fputc((char) byte,image->file);
  1191.   (void) free((char *) scanline);
  1192.   return(True);
  1193. }
  1194.  
  1195. /*
  1196. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1197. %                                                                             %
  1198. %                                                                             %
  1199. %                                                                             %
  1200. %   L Z W D e c o d e I m a g e                                               %
  1201. %                                                                             %
  1202. %                                                                             %
  1203. %                                                                             %
  1204. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1205. %
  1206. %  Function LZWDecodeImage uncompresses an image via LZW-coding.
  1207. %
  1208. %  The format of the LZWDecodeImage routine is:
  1209. %
  1210. %      status=LZWDecodeImage(image)
  1211. %
  1212. %  A description of each parameter follows:
  1213. %
  1214. %    o status:  Function LZWDecodeImage returns True if all the pixels are
  1215. %      uncompressed without error, otherwise False.
  1216. %
  1217. %    o image: The address of a structure of type Image.
  1218. %
  1219. %
  1220. */
  1221. unsigned int LZWDecodeImage(image)
  1222. Image
  1223.   *image;
  1224. {
  1225. #define MaxStackSize  4096
  1226. #define NullCode  (-1)
  1227.  
  1228.   int
  1229.     available,
  1230.     clear,
  1231.     code_mask,
  1232.     code_size,
  1233.     end_of_information,
  1234.     in_code,
  1235.     old_code;
  1236.  
  1237.   register int
  1238.     bits,
  1239.     code,
  1240.     count,
  1241.     i;
  1242.  
  1243.   register RunlengthPacket
  1244.     *p;
  1245.  
  1246.   register unsigned char
  1247.     *c;
  1248.  
  1249.   register unsigned int
  1250.     datum;
  1251.  
  1252.   short
  1253.     *prefix;
  1254.  
  1255.   unsigned char
  1256.     data_size,
  1257.     first,
  1258.     *packet,
  1259.     *pixel_stack,
  1260.     *suffix,
  1261.     *top_stack;
  1262.  
  1263.   /*
  1264.     Allocate decoder tables.
  1265.   */
  1266.   packet=(unsigned char *) malloc(256*sizeof(unsigned char));
  1267.   prefix=(short *) malloc(MaxStackSize*sizeof(short));
  1268.   suffix=(unsigned char *) malloc(MaxStackSize*sizeof(unsigned char));
  1269.   pixel_stack=(unsigned char *) malloc(MaxStackSize*sizeof(unsigned char));
  1270.   if ((packet == (unsigned char *) NULL) ||
  1271.       (prefix == (short *) NULL) ||
  1272.       (suffix == (unsigned char *) NULL) ||
  1273.       (pixel_stack == (unsigned char *) NULL))
  1274.     return(False);
  1275.   /*
  1276.     Initialize LZW data stream decoder.
  1277.   */
  1278.   data_size=fgetc(image->file);
  1279.   clear=1 << data_size;
  1280.   end_of_information=clear+1;
  1281.   available=clear+2;
  1282.   old_code=NullCode;
  1283.   code_size=data_size+1;
  1284.   code_mask=(1 << code_size)-1;
  1285.   for (code=0; code < clear; code++)
  1286.   {
  1287.     prefix[code]=0;
  1288.     suffix[code]=code;
  1289.   }
  1290.   /*
  1291.     Decode LZW pixel stream.
  1292.   */
  1293.   datum=0;
  1294.   bits=0;
  1295.   c=0;
  1296.   count=0;
  1297.   first=0;
  1298.   top_stack=pixel_stack;
  1299.   p=image->pixels;
  1300.   for (i=0; i < image->packets; )
  1301.   {
  1302.     if (top_stack == pixel_stack)
  1303.       {
  1304.         if (bits < code_size)
  1305.           {
  1306.             /*
  1307.               Load bytes until there is enough bits for a code.
  1308.             */
  1309.             if (count == 0)
  1310.               {
  1311.                 /*
  1312.                   Read a new data block.
  1313.                 */
  1314.                 count=ReadDataBlock((char *) packet,image->file);
  1315.                 if (count <= 0)
  1316.                   break;
  1317.                 c=packet;
  1318.               }
  1319.             datum+=(*c) << bits;
  1320.             bits+=8;
  1321.             c++;
  1322.             count--;
  1323.             continue;
  1324.           }
  1325.         /*
  1326.           Get the next code.
  1327.         */
  1328.         code=datum & code_mask;
  1329.         datum>>=code_size;
  1330.         bits-=code_size;
  1331.         /*
  1332.           Interpret the code
  1333.         */
  1334.         if ((code > available) || (code == end_of_information))
  1335.           break;
  1336.         if (code == clear)
  1337.           {
  1338.             /*
  1339.               Reset decoder.
  1340.             */
  1341.             code_size=data_size+1;
  1342.             code_mask=(1 << code_size)-1;
  1343.             available=clear+2;
  1344.             old_code=NullCode;
  1345.             continue;
  1346.           }
  1347.         if (old_code == NullCode)
  1348.           {
  1349.             *top_stack++=suffix[code];
  1350.             old_code=code;
  1351.             first=code;
  1352.             continue;
  1353.           }
  1354.         in_code=code;
  1355.         if (code == available)
  1356.           {
  1357.             *top_stack++=first;
  1358.             code=old_code;
  1359.           }
  1360.         while (code > clear)
  1361.         {
  1362.           *top_stack++=suffix[code];
  1363.           code=prefix[code];
  1364.         }
  1365.         first=suffix[code];
  1366.         /*
  1367.           Add a new string to the string table,
  1368.         */
  1369.         *top_stack++=first;
  1370.         prefix[available]=old_code;
  1371.         suffix[available]=first;
  1372.         available++;
  1373.         if (((available & code_mask) == 0) && (available < MaxStackSize))
  1374.           {
  1375.             code_size++;
  1376.             code_mask+=available;
  1377.           }
  1378.         old_code=in_code;
  1379.       }
  1380.     /*
  1381.       Pop a pixel off the pixel stack.
  1382.     */
  1383.     top_stack--;
  1384.     p->index=(unsigned short) *top_stack;
  1385.     p->length=0;
  1386.     p++;
  1387.     i++;
  1388.   }
  1389.   /*
  1390.     Initialize any remaining color packets to a known color.
  1391.   */
  1392.   for ( ; i < image->packets; i++)
  1393.   {
  1394.     p->index=0;
  1395.     p->length=0;
  1396.     p++;
  1397.   }
  1398.   SyncImage(image);
  1399.   /*
  1400.     Free decoder memory.
  1401.   */
  1402.   (void) free((char *) pixel_stack);
  1403.   (void) free((char *) suffix);
  1404.   (void) free((char *) prefix);
  1405.   (void) free((char *) packet);
  1406.   return(True);
  1407. }
  1408.  
  1409. /*
  1410. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1411. %                                                                             %
  1412. %                                                                             %
  1413. %                                                                             %
  1414. %   L Z W E n c o d e F i l t e r                                             %
  1415. %                                                                             %
  1416. %                                                                             %
  1417. %                                                                             %
  1418. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1419. %
  1420. %  Function LZWEncodeFilter compresses an image via LZW-coding specific to
  1421. %  Postscript Level II.
  1422. %
  1423. %  The format of the LZWEncodeFilter routine is:
  1424. %
  1425. %      status=LZWEncodeFilter(file,pixels,number_pixels)
  1426. %
  1427. %  A description of each parameter follows:
  1428. %
  1429. %    o status:  Function LZWEncodeFilter returns True if all the pixels are
  1430. %      compressed without error, otherwise False.
  1431. %
  1432. %    o file: The address of a structure of type FILE.  LZW encoded pixels
  1433. %      are written to this file.
  1434. %
  1435. %    o pixels: The address of an unsigned array of characters containing the
  1436. %      pixels to compress.
  1437. %
  1438. %    o number_pixels:  An unsigned interger that specifies the number of
  1439. %      pixels to compress.
  1440. %
  1441. %
  1442. */
  1443. unsigned int LZWEncodeFilter(file,pixels,number_pixels)
  1444. FILE
  1445.   *file;
  1446.  
  1447. unsigned char
  1448.   *pixels;
  1449.  
  1450. unsigned int
  1451.   number_pixels;
  1452. {
  1453. #define LZWClr  256  /* Clear Table Marker */
  1454. #define LZWEod  257  /* End of Data marker */
  1455. #define OutputCode(code) \
  1456. { \
  1457.     accumulator+=((long) code) << (32-code_width-number_bits); \
  1458.     number_bits+=code_width; \
  1459.     while (number_bits >= 8) \
  1460.     { \
  1461.         PrintByte(accumulator >> 24); \
  1462.         accumulator=accumulator << 8; \
  1463.         number_bits-=8; \
  1464.     } \
  1465. }
  1466. #define PrintByte(value)  \
  1467. { \
  1468.   (void) fprintf(file,"%02x",(int) (value & 0x0ff));  \
  1469.   count++;  \
  1470.   if (count >= 36)  \
  1471.     {  \
  1472.       (void) fputc('\n',file);  \
  1473.       count=0; \
  1474.     } \
  1475. }
  1476.  
  1477.   typedef struct _TableType
  1478.   {
  1479.     short
  1480.       prefix,
  1481.       suffix,
  1482.       next;
  1483.   } TableType;
  1484.  
  1485.   int
  1486.     index;
  1487.  
  1488.   register int
  1489.     i;
  1490.  
  1491.   short
  1492.     number_bits,
  1493.     code_width,
  1494.     count,
  1495.     last_code,
  1496.     next_index;
  1497.  
  1498.   TableType
  1499.     *table;
  1500.  
  1501.   unsigned long
  1502.     accumulator;
  1503.  
  1504.   /*
  1505.     Allocate string table.
  1506.   */
  1507.   table=(TableType *) malloc((1 << 12)*sizeof(TableType));
  1508.   if (table == (TableType *) NULL)
  1509.     return(False);
  1510.   /*
  1511.     Initialize variables.
  1512.   */
  1513.   accumulator=0;
  1514.   code_width=9;
  1515.   count=0;
  1516.   number_bits=0;
  1517.   last_code=0;
  1518.   OutputCode(LZWClr);
  1519.   for (index=0; index < 256; index++)
  1520.   {
  1521.     table[index].prefix=(-1);
  1522.     table[index].suffix=index;
  1523.     table[index].next=(-1);
  1524.   }
  1525.   next_index=LZWEod+1;
  1526.   code_width=9;
  1527.   last_code=pixels[0];
  1528.   for (i=1; i < number_pixels; i++)
  1529.   {
  1530.     /*
  1531.       Find string.
  1532.     */
  1533.     index=last_code;
  1534.     while (index != -1)
  1535.       if ((table[index].prefix != last_code) ||
  1536.           (table[index].suffix != pixels[i]))
  1537.         index=table[index].next;
  1538.       else
  1539.         {
  1540.           last_code=index;
  1541.           break;
  1542.         }
  1543.     if (last_code != index)
  1544.       {
  1545.         /*
  1546.           Add string.
  1547.         */
  1548.         OutputCode(last_code);
  1549.         table[next_index].prefix=last_code;
  1550.         table[next_index].suffix=pixels[i];
  1551.         table[next_index].next=table[last_code].next;
  1552.         table[last_code].next=next_index;
  1553.         next_index++;
  1554.         /*
  1555.           Did we just move up to next bit width?
  1556.         */
  1557.         if ((next_index >> code_width) != 0)
  1558.           {
  1559.             code_width++;
  1560.             if (code_width > 12)
  1561.               {
  1562.                 /*
  1563.                   Did we overflow the max bit width?
  1564.                 */
  1565.                 code_width--;
  1566.                 OutputCode(LZWClr);
  1567.                 for (index=0; index < 256; index++)
  1568.                 {
  1569.                   table[index].prefix=(-1);
  1570.                   table[index].suffix=index;
  1571.                   table[index].next=(-1);
  1572.                 }
  1573.                 next_index=LZWEod+1;
  1574.                 code_width=9;
  1575.               }
  1576.             }
  1577.           last_code=pixels[i];
  1578.       }
  1579.   }
  1580.   /*
  1581.     Flush tables.
  1582.   */
  1583.   OutputCode(last_code);
  1584.   OutputCode(LZWEod);
  1585.   if (number_bits != 0)
  1586.     PrintByte(accumulator >> 24);
  1587.   (void) free(table);
  1588.   return(True);
  1589. }
  1590.  
  1591. /*
  1592. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1593. %                                                                             %
  1594. %                                                                             %
  1595. %                                                                             %
  1596. %   L Z W E n c o d e I m a g e                                               %
  1597. %                                                                             %
  1598. %                                                                             %
  1599. %                                                                             %
  1600. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1601. %
  1602. %  Function LZWEncodeImage compresses an image via LZW-coding.
  1603. %
  1604. %  The format of the LZWEncodeImage routine is:
  1605. %
  1606. %      status=LZWEncodeImage(image,data_size)
  1607. %
  1608. %  A description of each parameter follows:
  1609. %
  1610. %    o status:  Function LZWEncodeImage returns True if all the pixels are
  1611. %      compressed without error, otherwise False.
  1612. %
  1613. %    o image: The address of a structure of type Image.
  1614. %
  1615. %
  1616. */
  1617. unsigned int LZWEncodeImage(image,data_size)
  1618. Image
  1619.   *image;
  1620.  
  1621. unsigned int
  1622.   data_size;
  1623. {
  1624. #define MaxCode(number_bits)  ((1 << (number_bits))-1)
  1625. #define MaxHashTable  5003
  1626. #define MaxLZWBits  12
  1627. #define MaxLZWTable  (1 << MaxLZWBits)
  1628. #define LZWOutputCode(code) \
  1629. { \
  1630.   /*  \
  1631.     Emit a code. \
  1632.   */ \
  1633.   if (bits > 0) \
  1634.     datum|=((long) code << bits); \
  1635.   else \
  1636.     datum=(long) code; \
  1637.   bits+=number_bits; \
  1638.   while (bits >= 8)  \
  1639.   { \
  1640.     /*  \
  1641.       Add a character to current packet. \
  1642.     */ \
  1643.     packet[byte_count++]=(unsigned char) (datum & 0xff); \
  1644.     if (byte_count >= 254) \
  1645.       { \
  1646.         (void) fputc(byte_count,image->file); \
  1647.         (void) fwrite((char *) packet,1,byte_count,image->file); \
  1648.         byte_count=0; \
  1649.       } \
  1650.     datum>>=8; \
  1651.     bits-=8; \
  1652.   } \
  1653.   if (free_code > max_code)  \
  1654.     { \
  1655.       number_bits++; \
  1656.       if (number_bits == MaxLZWBits) \
  1657.         max_code=MaxLZWTable; \
  1658.       else \
  1659.         max_code=MaxCode(number_bits); \
  1660.     } \
  1661. }
  1662.  
  1663.   int
  1664.     bits,
  1665.     byte_count,
  1666.     next_pixel,
  1667.     number_bits;
  1668.  
  1669.   long
  1670.     datum;
  1671.  
  1672.   register int
  1673.     displacement,
  1674.     i,
  1675.     j;
  1676.  
  1677.   register RunlengthPacket
  1678.     *p;
  1679.  
  1680.   short
  1681.     clear_code,
  1682.     end_of_information_code,
  1683.     free_code,
  1684.     *hash_code,
  1685.     *hash_prefix,
  1686.     index,
  1687.     max_code,
  1688.     waiting_code;
  1689.  
  1690.   unsigned char
  1691.     *packet,
  1692.     *hash_suffix;
  1693.  
  1694.   /*
  1695.     Uncompress image.
  1696.   */
  1697.   if (!UncompressImage(image))
  1698.     return(False);
  1699.   /*
  1700.     Allocate encoder tables.
  1701.   */
  1702.   packet=(unsigned char *) malloc(256*sizeof(unsigned char));
  1703.   hash_code=(short *) malloc(MaxHashTable*sizeof(short));
  1704.   hash_prefix=(short *) malloc(MaxHashTable*sizeof(short));
  1705.   hash_suffix=(unsigned char *) malloc(MaxHashTable*sizeof(unsigned char));
  1706.   if ((packet == (unsigned char *) NULL) || (hash_code == (short *) NULL) ||
  1707.       (hash_prefix == (short *) NULL) ||
  1708.       (hash_suffix == (unsigned char *) NULL))
  1709.     return(False);
  1710.   /*
  1711.     Initialize LZW encoder.
  1712.   */
  1713.   number_bits=data_size;
  1714.   max_code=MaxCode(number_bits);
  1715.   clear_code=((short) 1 << (data_size-1));
  1716.   end_of_information_code=clear_code+1;
  1717.   free_code=clear_code+2;
  1718.   byte_count=0;
  1719.   datum=0;
  1720.   bits=0;
  1721.   for (i=0; i < MaxHashTable; i++)
  1722.     hash_code[i]=0;
  1723.   LZWOutputCode(clear_code);
  1724.   /*
  1725.     Encode pixels.
  1726.   */
  1727.   p=image->pixels;
  1728.   waiting_code=p->index;
  1729.   for (i=1; i < (image->columns*image->rows); i++)
  1730.   {
  1731.     /*
  1732.       Probe hash table.
  1733.     */
  1734.     p++;
  1735.     index=p->index & 0xff;
  1736.     j=(int) ((int) index << (MaxLZWBits-8))+waiting_code;
  1737.     if (j >= MaxHashTable)
  1738.       j-=MaxHashTable;
  1739.     if (hash_code[j] > 0)
  1740.       {
  1741.         if ((hash_prefix[j] == waiting_code) && (hash_suffix[j] == index))
  1742.           {
  1743.             waiting_code=hash_code[j];
  1744.             continue;
  1745.           }
  1746.         if (j == 0)
  1747.           displacement=1;
  1748.         else
  1749.           displacement=MaxHashTable-j;
  1750.         next_pixel=False;
  1751.         for ( ; ; )
  1752.         {
  1753.           j-=displacement;
  1754.           if (j < 0)
  1755.             j+=MaxHashTable;
  1756.           if (hash_code[j] == 0)
  1757.             break;
  1758.           if ((hash_prefix[j] == waiting_code) && (hash_suffix[j] == index))
  1759.             {
  1760.               waiting_code=hash_code[j];
  1761.               next_pixel=True;
  1762.               break;
  1763.             }
  1764.         }
  1765.         if (next_pixel == True)
  1766.           continue;
  1767.       }
  1768.     LZWOutputCode(waiting_code);
  1769.     if (free_code < MaxLZWTable)
  1770.       {
  1771.         hash_code[j]=free_code++;
  1772.         hash_prefix[j]=waiting_code;
  1773.         hash_suffix[j]=index;
  1774.       }
  1775.     else
  1776.       {
  1777.         /*
  1778.           Fill the hash table with empty entries.
  1779.         */
  1780.         for (j=0; j < MaxHashTable; j++)
  1781.           hash_code[j]=0;
  1782.         /*
  1783.           Reset compressor and issue a clear code.
  1784.         */
  1785.         free_code=clear_code+2;
  1786.         LZWOutputCode(clear_code);
  1787.         number_bits=data_size;
  1788.         max_code=MaxCode(number_bits);
  1789.       }
  1790.     waiting_code=index;
  1791.   }
  1792.   /*
  1793.     Flush out the buffered code.
  1794.   */
  1795.   LZWOutputCode(waiting_code);
  1796.   LZWOutputCode(end_of_information_code);
  1797.   if (bits > 0)
  1798.     {
  1799.       /*
  1800.         Add a character to current packet.
  1801.       */
  1802.       packet[byte_count++]=(unsigned char) (datum & 0xff);
  1803.       if (byte_count >= 254)
  1804.         {
  1805.           (void) fputc(byte_count,image->file);
  1806.           (void) fwrite((char *) packet,1,byte_count,image->file);
  1807.           byte_count=0;
  1808.         }
  1809.     }
  1810.   /*
  1811.     Flush accumulated data.
  1812.   */
  1813.   if (byte_count > 0)
  1814.     {
  1815.       (void) fputc(byte_count,image->file);
  1816.       (void) fwrite((char *) packet,1,byte_count,image->file);
  1817.     }
  1818.   /*
  1819.     Free encoder memory.
  1820.   */
  1821.   (void) free((char *) hash_suffix);
  1822.   (void) free((char *) hash_prefix);
  1823.   (void) free((char *) hash_code);
  1824.   (void) free((char *) packet);
  1825.   if (i < image->packets)
  1826.     return(False);
  1827.   return(True);
  1828. }
  1829.  
  1830. /*
  1831. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1832. %                                                                             %
  1833. %                                                                             %
  1834. %                                                                             %
  1835. %   P a c k b i t s E n c o d e I m a g e                                     %
  1836. %                                                                             %
  1837. %                                                                             %
  1838. %                                                                             %
  1839. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1840. %
  1841. %  Function PackbitsEncodeImage compresses an image via Macintosh pack bits
  1842. %  encoding.
  1843. %
  1844. %  The format of the PackbitsEncodeImage routine is:
  1845. %
  1846. %      status=PackbitsEncodeImage(image,scanline,packbits)
  1847. %
  1848. %  A description of each parameter follows:
  1849. %
  1850. %    o status:  Function PackbitsEncodeImage returns True if all the pixels are
  1851. %      compressed without error, otherwise False.
  1852. %
  1853. %    o image: The address of a structure of type Image.
  1854. %
  1855. %    o scanline: A pointer to an array of characters to pack.
  1856. %
  1857. %    o packbits: A pointer to an array of characters where the packed
  1858. %      characters are stored.
  1859. %
  1860. %
  1861. */
  1862. unsigned int PackbitsEncodeImage(image,scanline,packbits)
  1863. Image
  1864.   *image;
  1865.  
  1866. unsigned char
  1867.   *scanline,
  1868.   *packbits;
  1869. {
  1870. #define MaxCount  128
  1871. #define MaxPackBitsRunlength  128
  1872.  
  1873.   int
  1874.     count,
  1875.     number_packets,
  1876.     repeat_count,
  1877.     runlength;
  1878.  
  1879.   register int
  1880.     i;
  1881.  
  1882.   register unsigned char
  1883.     *p,
  1884.     *q;
  1885.  
  1886.   unsigned char
  1887.     index;
  1888.  
  1889.   unsigned int
  1890.     bytes_per_line;
  1891.  
  1892.   /*
  1893.     Pack scanline.
  1894.   */
  1895.   bytes_per_line=image->columns;
  1896.   if (image->class == DirectClass)
  1897.     bytes_per_line*=3;
  1898.   count=0;
  1899.   runlength=0;
  1900.   p=scanline+(bytes_per_line-1);
  1901.   q=packbits;
  1902.   index=(*p);
  1903.   for (i=bytes_per_line-1; i >= 0; i--)
  1904.   {
  1905.     if (index == *p)
  1906.       runlength++;
  1907.     else
  1908.       {
  1909.         if (runlength < 3)
  1910.           while (runlength > 0)
  1911.           {
  1912.             *q++=index;
  1913.             runlength--;
  1914.             count++;
  1915.             if (count == MaxCount)
  1916.               {
  1917.                 *q++=MaxCount-1;
  1918.                 count-=MaxCount;
  1919.               }
  1920.           }
  1921.         else
  1922.           {
  1923.             if (count > 0)
  1924.               *q++=count-1;
  1925.             count=0;
  1926.             while (runlength > 0)
  1927.             {
  1928.               repeat_count=runlength;
  1929.               if (repeat_count > MaxPackBitsRunlength)
  1930.                 repeat_count=MaxPackBitsRunlength;
  1931.               *q++=index;
  1932.               *q++=257-repeat_count;
  1933.               runlength-=repeat_count;
  1934.             }
  1935.           }
  1936.         runlength=1;
  1937.       }
  1938.     index=(*p);
  1939.     p--;
  1940.   }
  1941.   if (runlength < 3)
  1942.     while (runlength > 0)
  1943.     {
  1944.       *q++=index;
  1945.       runlength--;
  1946.       count++;
  1947.       if (count == MaxCount)
  1948.         {
  1949.           *q++=MaxCount-1;
  1950.           count-=MaxCount;
  1951.         }
  1952.     }
  1953.   else
  1954.     {
  1955.       if (count > 0)
  1956.         *q++=count-1;
  1957.       count=0;
  1958.       while (runlength > 0)
  1959.       {
  1960.         repeat_count=runlength;
  1961.         if (repeat_count > MaxPackBitsRunlength)
  1962.           repeat_count=MaxPackBitsRunlength;
  1963.         *q++=index;
  1964.         *q++=257-repeat_count;
  1965.         runlength-=repeat_count;
  1966.       }
  1967.     }
  1968.   if (count > 0)
  1969.     *q++=count-1;
  1970.   /*
  1971.     Write the number of and the packed packets.
  1972.   */
  1973.   number_packets=q-packbits;
  1974.   if ((bytes_per_line-1) > 250)
  1975.     {
  1976.       MSBFirstWriteShort((unsigned short) number_packets,image->file);
  1977.       number_packets+=2;
  1978.     }
  1979.   else
  1980.     {
  1981.       index=number_packets;
  1982.       (void) fputc((char) index,image->file);
  1983.       number_packets++;
  1984.     }
  1985.   while (q != packbits)
  1986.   {
  1987.     q--;
  1988.     (void) fputc((char) *q,image->file);
  1989.   }
  1990.   return(number_packets);
  1991. }
  1992.  
  1993. /*
  1994. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1995. %                                                                             %
  1996. %                                                                             %
  1997. %                                                                             %
  1998. %   Q D e c o d e I m a g e                                                   %
  1999. %                                                                             %
  2000. %                                                                             %
  2001. %                                                                             %
  2002. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2003. %
  2004. %  Function QDecodeImage uncompresses an image via Q-coding.
  2005. %
  2006. %  The format of the QDecodeImage routine is:
  2007. %
  2008. %      count=QDecodeImage(compressed_pixels,pixels,number_columns,number_rows)
  2009. %
  2010. %  A description of each parameter follows:
  2011. %
  2012. %    o count:  The QDecodeImage routine returns this integer value.  It is
  2013. %      the actual number of pixels created by the decompression process.
  2014. %
  2015. %    o compressed_pixels:  The address of a byte (8 bits) array of compressed
  2016. %      pixel data.
  2017. %
  2018. %    o pixels:  The address of a byte (8 bits) array of pixel data created by
  2019. %      the uncompression process.  The number of bytes in this array
  2020. %      must be at least equal to the number columns times the number of rows
  2021. %      of the source pixels.
  2022. %
  2023. %    o number_columns:  An integer value that is the number of columns or
  2024. %      width in pixels of your source image.
  2025. %
  2026. %    o number_rows:  An integer value that is the number of rows or
  2027. %      heigth in pixels of your source image.
  2028. %
  2029. %
  2030. */
  2031. unsigned int QDecodeImage(compressed_pixels,pixels,number_columns,number_rows)
  2032. unsigned char
  2033.   *compressed_pixels,
  2034.   *pixels;
  2035.  
  2036. unsigned int
  2037.   number_columns,
  2038.   number_rows;
  2039. {
  2040.   int
  2041.     decision,
  2042.     i,
  2043.     prediction,
  2044.     row;
  2045.  
  2046.   register int
  2047.     column,
  2048.     magnitude,
  2049.     sign,
  2050.     state,
  2051.     value;
  2052.  
  2053.   register ScanlinePacket
  2054.     *cs,
  2055.     *ls;
  2056.  
  2057.   register unsigned char
  2058.     *p;
  2059.  
  2060.   ScanlinePacket
  2061.     *scanline;
  2062.  
  2063.   for (i=0; i < MaxContextStates; i++)
  2064.   {
  2065.     probability_estimate[i]=0;
  2066.     more_probable[i]=0;
  2067.     less_probable[i]=1;
  2068.   }
  2069.   /*
  2070.     Allocate scanline for row values and states
  2071.   */
  2072.   scanline=(ScanlinePacket *)
  2073.     malloc((((number_columns+1) << 1)*sizeof(ScanlinePacket)));
  2074.   if (scanline == (ScanlinePacket *) NULL)
  2075.     {
  2076.       Warning("Unable to compress image, Unable to allocate memory",
  2077.         (char *) NULL);
  2078.       exit(1);
  2079.     }
  2080.   cs=scanline;
  2081.   for (i=0; i < ((number_columns+1) << 1); i++)
  2082.   {
  2083.     cs->pixel=0;
  2084.     cs->state=ZeroState;
  2085.     cs++;
  2086.   }
  2087.   interval=MinimumIntervalD;
  2088.   p=pixels;
  2089.   q=compressed_pixels+1;
  2090.   /*
  2091.     Add a new unsigned char of compressed data to the Code register.
  2092.   */
  2093.   code=(int) (*q) << 16;
  2094.   if ((*q++) == 0xff)
  2095.     code+=((int) (*q) << 9)+0x02;
  2096.   else
  2097.     code+=((*q) << 8)+0x01;
  2098.   code<<=4;
  2099.   code+=(interval << 16);
  2100.   /*
  2101.     Decode each image scanline.
  2102.   */
  2103.   for (row=0; row < number_rows; row++)
  2104.   {
  2105.     ls=scanline+(number_columns+1)*((row+0) % 2);
  2106.     cs=scanline+(number_columns+1)*((row+1) % 2);
  2107.     for (column=0; column < number_columns; column++)
  2108.     {
  2109.       prediction=(int) cs->pixel-(int) ls->pixel;
  2110.       ls++;
  2111.       prediction+=(int) ls->pixel;
  2112.       state=statistics[cs->state][ls->state];
  2113.       cs++;
  2114.       cs->state=ZeroState;
  2115.       /*
  2116.         Branch for zero code value
  2117.       */
  2118.       Decode(state,&decision);
  2119.       if (decision == No)
  2120.         value=0;
  2121.       else
  2122.         {
  2123.           /*
  2124.             Decode sign information
  2125.           */
  2126.           state++;
  2127.           Decode(state,&decision);
  2128.           if (decision == Yes)
  2129.             sign=(-1);
  2130.           else
  2131.             {
  2132.               sign=1;
  2133.               state++;
  2134.             }
  2135.           state++;
  2136.           /*
  2137.             Branch for value=+-1
  2138.           */
  2139.           Decode(state,&decision);
  2140.           if (decision == No)
  2141.             value=1;
  2142.           else
  2143.             {
  2144.               /*
  2145.                 Establish magnitude of value.
  2146.               */
  2147.               magnitude=2;
  2148.               state=100;
  2149.               Decode(state,&decision);
  2150.               while (decision != No)
  2151.               {
  2152.                 if (state < 107)
  2153.                   state++;
  2154.                 magnitude<<=1;
  2155.                 Decode(state,&decision);
  2156.               }
  2157.               /*
  2158.                 Code remaining bits.
  2159.               */
  2160.               state+=7;
  2161.               value=1;
  2162.               magnitude>>=2;
  2163.               if (magnitude != 0)
  2164.                 {
  2165.                   Decode(state,&decision);
  2166.                   state+=6;
  2167.                   value=(value << 1) | decision;
  2168.                   magnitude>>=1;
  2169.                   while (magnitude != 0)
  2170.                   {
  2171.                     Decode(state,&decision);
  2172.                     value=(value << 1) | decision;
  2173.                     magnitude>>=1;
  2174.                   }
  2175.                 }
  2176.               value++;
  2177.             }
  2178.           if (value > LowerBound)
  2179.             if (value <= UpperBound)
  2180.               cs->state=
  2181.                 (sign < ZeroState ? SmallPostitiveState : SmallNegativeState);
  2182.             else
  2183.               cs->state=
  2184.                 (sign < ZeroState ? LargePostitiveState : LargeNegativeState);
  2185.           if (sign < 0)
  2186.             value=(-value);
  2187.         }
  2188.       cs->pixel=(unsigned char) (value+prediction);
  2189.       *p++=cs->pixel;
  2190.     }
  2191.   }
  2192.   (void) free((char *) scanline);
  2193.   return((unsigned int) (p-pixels));
  2194. }
  2195.  
  2196. /*
  2197. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2198. %                                                                             %
  2199. %                                                                             %
  2200. %                                                                             %
  2201. %   Q E n c o d e I m a g e                                                   %
  2202. %                                                                             %
  2203. %                                                                             %
  2204. %                                                                             %
  2205. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2206. %
  2207. %  Function QEncodeImage compresses an image via q-coding.  QEncodeImage uses
  2208. %  a simple predictive method.  The predictor combines three neighboring
  2209. %  samples (A, B, and C) to form a prediction of the sample X:
  2210. %
  2211. %    C B
  2212. %    A X
  2213. %
  2214. %  The prediction formula is A + B - C.  The prediction is subtracted from
  2215. %  from the actual sample X and the difference is encoded by an arithmetic
  2216. %  entropy coding method.
  2217. %
  2218. %  The format of the QEncodeImage routine is:
  2219. %
  2220. %      count=QEncodeImage(pixels,compressed_pixels,number_columns,number_rows)
  2221. %
  2222. %  A description of each parameter follows:
  2223. %
  2224. %    o count:  The QEncodeImage routine returns this integer value.  It is
  2225. %      the actual number of compressed pixels created by the compression
  2226. %      process.
  2227. %
  2228. %    o pixels:  The address of a byte (8 bits) array of pixel data.
  2229. %
  2230. %    o compressed_pixels:  The address of a byte (8 bits) array of pixel data
  2231. %      created by the compression process.  The number of bytes in this array
  2232. %      must be at least equal to the number columns times the number of rows
  2233. %      of the source pixels to allow for the possibility that no compression
  2234. %      is possible.  The actual number of bytes used is reflected by the
  2235. %      count parameter.
  2236. %
  2237. %    o number_columns:  An integer value that is the number of columns or
  2238. %      width in pixels of your source image.
  2239. %
  2240. %    o number_rows:  An integer value that is the number of rows or
  2241. %      heigth in pixels of your source image.
  2242. %
  2243. %
  2244. %
  2245. */
  2246. unsigned int QEncodeImage(pixels,compressed_pixels,number_columns,number_rows)
  2247. unsigned char
  2248.   *pixels,
  2249.   *compressed_pixels;
  2250.  
  2251. unsigned int
  2252.   number_columns,
  2253.   number_rows;
  2254. {
  2255.   int
  2256.     i,
  2257.     prediction,
  2258.     row;
  2259.  
  2260.   register int
  2261.     column,
  2262.     magnitude,
  2263.     sign,
  2264.     state,
  2265.     value;
  2266.  
  2267.   register ScanlinePacket
  2268.     *cs,
  2269.     *ls;
  2270.  
  2271.   register unsigned char
  2272.     *p;
  2273.  
  2274.   ScanlinePacket
  2275.     *scanline;
  2276.  
  2277.   void
  2278.     Flush();
  2279.  
  2280.   for (i=0; i < MaxContextStates; i++)
  2281.   {
  2282.     probability_estimate[i]=0;
  2283.     more_probable[i]=0;
  2284.   }
  2285.   /*
  2286.     Allocate scanline for row values and states.
  2287.   */
  2288.   scanline=(ScanlinePacket *)
  2289.     malloc(((number_columns+1) << 1)*sizeof(ScanlinePacket));
  2290.   if (scanline == (ScanlinePacket *) NULL)
  2291.     {
  2292.       Warning("Unable to compress image, Unable to allocate memory",
  2293.         (char *) NULL);
  2294.       exit(1);
  2295.     }
  2296.   cs=scanline;
  2297.   for (i=0; i < ((number_columns+1) << 1); i++)
  2298.   {
  2299.     cs->pixel=0;
  2300.     cs->state=ZeroState;
  2301.     cs++;
  2302.   }
  2303.   interval=MinimumIntervalE;
  2304.   p=pixels;
  2305.   q=compressed_pixels;
  2306.   (*q)++;
  2307.   code=0x00180000;
  2308.   /*
  2309.     Encode each scanline.
  2310.   */
  2311.   for (row=0; row < number_rows; row++)
  2312.   {
  2313.     ls=scanline+(number_columns+1)*((row+0) % 2);
  2314.     cs=scanline+(number_columns+1)*((row+1) % 2);
  2315.     for (column=0; column < number_columns; column++)
  2316.     {
  2317.       prediction=(int) cs->pixel-(int) ls->pixel;
  2318.       ls++;
  2319.       prediction+=(int) ls->pixel;
  2320.       state=statistics[cs->state][ls->state];
  2321.       cs++;
  2322.       cs->pixel=(*p++);
  2323.       cs->state=ZeroState;
  2324.       value=(int) cs->pixel-prediction;
  2325.       Encode(state,(value == 0 ? No : Yes));
  2326.       if (value != 0)
  2327.         {
  2328.           /*
  2329.             Code sign information
  2330.           */
  2331.           state++;
  2332.           sign=(value < 0 ? -1 : 1);
  2333.           Encode(state,(sign >= 0 ? No : Yes));
  2334.           if (sign < 0)
  2335.             value=(-value);
  2336.           else
  2337.             state++;
  2338.           state++;
  2339.           value--;
  2340.           /*
  2341.             Branch for code=+-1
  2342.           */
  2343.           Encode(state,(value == 0 ? No : Yes));
  2344.           if (value != 0)
  2345.             {
  2346.               /*
  2347.                 Establish magnitude of value.
  2348.               */
  2349.               state=100;
  2350.               magnitude=2;
  2351.               while (value >= magnitude)
  2352.               {
  2353.                 Encode(state,Yes);
  2354.                 if (state < 107)
  2355.                   state++;
  2356.                 magnitude<<=1;
  2357.               }
  2358.               Encode(state,No);
  2359.               /*
  2360.                 Code remaining bits
  2361.               */
  2362.               state+=7;
  2363.               magnitude>>=2;
  2364.               if (magnitude != 0)
  2365.                 {
  2366.                   Encode(state,((magnitude & value) == 0 ? No : Yes));
  2367.                   state+=6;
  2368.                   magnitude>>=1;
  2369.                   while (magnitude != 0)
  2370.                   {
  2371.                     Encode(state,((magnitude & value) == 0 ? No : Yes));
  2372.                     magnitude>>=1;
  2373.                   }
  2374.                 }
  2375.             }
  2376.           if (value >= LowerBound)
  2377.             if (value < UpperBound)
  2378.               cs->state=
  2379.                 (sign < ZeroState ? SmallPostitiveState : SmallNegativeState);
  2380.             else
  2381.               cs->state=
  2382.                 (sign < ZeroState ? LargePostitiveState : LargeNegativeState);
  2383.         }
  2384.     }
  2385.   }
  2386.   Flush();
  2387.   (void) free((char *) scanline);
  2388.   return((unsigned int) (q-compressed_pixels));
  2389. }
  2390.  
  2391. /*
  2392. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2393. %                                                                             %
  2394. %                                                                             %
  2395. %                                                                             %
  2396. %   R u n l e n g t h D e c o d e I m a g e                                   %
  2397. %                                                                             %
  2398. %                                                                             %
  2399. %                                                                             %
  2400. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2401. %
  2402. %  Function RunlengthDecodeImage unpacks the packed image pixels into
  2403. %  runlength-encoded pixel packets.  The packed image pixel memory is then
  2404. %  freed.
  2405. %
  2406. %  The format of the RunlengthDecodeImage routine is:
  2407. %
  2408. %      status=RunlengthDecodeImage(image)
  2409. %
  2410. %  A description of each parameter follows:
  2411. %
  2412. %    o status: Function RunlengthDecodeImage return True if the image is
  2413. %      decoded.  False is returned if there is an error occurs.
  2414. %
  2415. %    o image: The address of a structure of type Image.
  2416. %
  2417. %
  2418. */
  2419. unsigned int RunlengthDecodeImage(image)
  2420. Image
  2421.   *image;
  2422. {
  2423.   register int
  2424.     i;
  2425.  
  2426.   register RunlengthPacket
  2427.     *q;
  2428.  
  2429.   register unsigned char
  2430.     *p;
  2431.  
  2432.   unsigned long
  2433.     count;
  2434.  
  2435.   if (image->packed_pixels == (unsigned char *) NULL)
  2436.     {
  2437.       Warning("Unable to unpack pixels","no packed image pixels");
  2438.       return(False);
  2439.     }
  2440.   /*
  2441.     Allocate pixels.
  2442.   */
  2443.   if (image->pixels != (RunlengthPacket *) NULL)
  2444.     (void) free((char *) image->pixels);
  2445.   image->pixels=(RunlengthPacket *)
  2446.     malloc((unsigned int) image->packets*sizeof(RunlengthPacket));
  2447.   if (image->pixels == (RunlengthPacket *) NULL)
  2448.     {
  2449.       Warning("Unable to unpack pixels","Memory allocation failed");
  2450.       return(False);
  2451.     }
  2452.   /*
  2453.     Unpack the packed image pixels into runlength-encoded pixel packets.
  2454.   */
  2455.   p=image->packed_pixels;
  2456.   q=image->pixels;
  2457.   count=0;
  2458.   if (image->class == DirectClass)
  2459.     {
  2460.       register int
  2461.         alpha;
  2462.  
  2463.       alpha=image->alpha;
  2464.       if (image->compression == RunlengthEncodedCompression)
  2465.         for (i=0; i < image->packets; i++)
  2466.         {
  2467.           q->red=(*p++);
  2468.           q->green=(*p++);
  2469.           q->blue=(*p++);
  2470.           q->index=(unsigned short) (alpha ? (*p++) : 0);
  2471.           q->length=(*p++);
  2472.           count+=(q->length+1);
  2473.           q++;
  2474.         }
  2475.       else
  2476.         for (i=0; i < image->packets; i++)
  2477.         {
  2478.           q->red=(*p++);
  2479.           q->green=(*p++);
  2480.           q->blue=(*p++);
  2481.           q->index=(unsigned short) (alpha ? (*p++) : 0);
  2482.           q->length=0;
  2483.           count++;
  2484.           q++;
  2485.         }
  2486.     }
  2487.   else
  2488.     {
  2489.       register unsigned short
  2490.         index;
  2491.  
  2492.       if (image->compression == RunlengthEncodedCompression)
  2493.         {
  2494.           if (image->colors <= 256)
  2495.             for (i=0; i < image->packets; i++)
  2496.             {
  2497.               q->index=(unsigned short) (*p++);
  2498.               q->length=(*p++);
  2499.               count+=(q->length+1);
  2500.               q++;
  2501.             }
  2502.           else
  2503.             for (i=0; i < image->packets; i++)
  2504.             {
  2505.               index=(*p++) << 8;
  2506.               index|=(*p++);
  2507.               q->index=index;
  2508.               q->length=(*p++);
  2509.               count+=(q->length+1);
  2510.               q++;
  2511.             }
  2512.         }
  2513.       else
  2514.         if (image->colors <= 256)
  2515.           for (i=0; i < image->packets; i++)
  2516.           {
  2517.             q->index=(unsigned short) (*p++);
  2518.             q->length=0;
  2519.             count++;
  2520.             q++;
  2521.           }
  2522.         else
  2523.           for (i=0; i < image->packets; i++)
  2524.           {
  2525.             index=(*p++) << 8;
  2526.             index|=(*p++);
  2527.             q->index=index;
  2528.             q->length=0;
  2529.             count++;
  2530.             q++;
  2531.           }
  2532.       SyncImage(image);
  2533.     }
  2534.   /*
  2535.     Free packed pixels memory.
  2536.   */
  2537.   (void) free((char *) image->packed_pixels);
  2538.   image->packed_pixels=(unsigned char *) NULL;
  2539.   /*
  2540.     Guarentee the correct number of pixel packets.
  2541.   */
  2542.   if (count > (image->columns*image->rows))
  2543.     {
  2544.       Warning("insufficient image data in file",image->filename);
  2545.       return(False);
  2546.     }
  2547.   else
  2548.     if (count < (image->columns*image->rows))
  2549.       {
  2550.         Warning("too much image data in file",image->filename);
  2551.         return(False);
  2552.       }
  2553.   return(True);
  2554. }
  2555.  
  2556. /*
  2557. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2558. %                                                                             %
  2559. %                                                                             %
  2560. %                                                                             %
  2561. %   R u n l e n g t h E n c o d e I m a g e                                   %
  2562. %                                                                             %
  2563. %                                                                             %
  2564. %                                                                             %
  2565. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2566. %
  2567. %  Function RunlengthEncodeImage packs the runlength-encoded pixel packets
  2568. %  into the minimum number of bytes.
  2569. %
  2570. %  The format of the RunlengthEncodeImage routine is:
  2571. %
  2572. %      status=RunlengthEncodeImage(image)
  2573. %
  2574. %  A description of each parameter follows:
  2575. %
  2576. %    o status: Function RunlengthEncodeImage return True if the image is
  2577. %      packed.  False is returned if an error occurs.
  2578. %
  2579. %    o image: The address of a structure of type Image.
  2580. %
  2581. %
  2582. */
  2583. unsigned int RunlengthEncodeImage(image)
  2584. Image
  2585.   *image;
  2586. {
  2587.   register int
  2588.     i,
  2589.     j;
  2590.  
  2591.   register RunlengthPacket
  2592.     *p;
  2593.  
  2594.   register unsigned char
  2595.     *q;
  2596.  
  2597.   unsigned long
  2598.     count,
  2599.     packets;
  2600.  
  2601.   if (image->pixels == (RunlengthPacket *) NULL)
  2602.     {
  2603.       Warning("Unable to pack pixels","no image pixels");
  2604.       return(False);
  2605.     }
  2606.   /*
  2607.     Runlength-encode only if it consumes less memory than no compression.
  2608.   */
  2609.   if (image->compression == RunlengthEncodedCompression)
  2610.     CompressImage(image);
  2611.   /*
  2612.     Determine packed packet size.
  2613.   */
  2614.   if (image->class == PseudoClass)
  2615.     {
  2616.       image->packet_size=1;
  2617.       if (image->colors > 256)
  2618.         image->packet_size++;
  2619.     }
  2620.   else
  2621.     {
  2622.       image->packet_size=3;
  2623.       if (image->alpha)
  2624.         image->packet_size++;
  2625.     }
  2626.   if (image->compression == RunlengthEncodedCompression)
  2627.     image->packet_size++;
  2628.   /*
  2629.     Allocate packed pixel memory.
  2630.   */
  2631.   if (image->packed_pixels != (unsigned char *) NULL)
  2632.     (void) free((char *) image->packed_pixels);
  2633.   packets=image->packets;
  2634.   if (image->compression != RunlengthEncodedCompression)
  2635.     packets=image->columns*image->rows;
  2636.   image->packed_pixels=(unsigned char *)
  2637.     malloc((unsigned int) packets*image->packet_size*sizeof(unsigned char));
  2638.   if (image->packed_pixels == (unsigned char *) NULL)
  2639.     {
  2640.       Warning("Unable to pack pixels","Memory allocation failed");
  2641.       return(False);
  2642.     }
  2643.   /*
  2644.     Packs the runlength-encoded pixel packets into the minimum number of bytes.
  2645.   */
  2646.   p=image->pixels;
  2647.   q=image->packed_pixels;
  2648.   count=0;
  2649.   if (image->class == DirectClass)
  2650.     {
  2651.       register int
  2652.         alpha;
  2653.  
  2654.       alpha=image->alpha;
  2655.       if (image->compression == RunlengthEncodedCompression)
  2656.         for (i=0; i < image->packets; i++)
  2657.         {
  2658.           *q++=p->red;
  2659.           *q++=p->green;
  2660.           *q++=p->blue;
  2661.           if (alpha)
  2662.             *q++=(unsigned char) p->index;
  2663.           *q++=p->length;
  2664.           count+=(p->length+1);
  2665.           p++;
  2666.         }
  2667.       else
  2668.         for (i=0; i < image->packets; i++)
  2669.         {
  2670.           for (j=0; j <= ((int) p->length); j++)
  2671.           {
  2672.             *q++=p->red;
  2673.             *q++=p->green;
  2674.             *q++=p->blue;
  2675.             if (alpha)
  2676.               *q++=(unsigned char) p->index;
  2677.           }
  2678.           count+=(p->length+1);
  2679.           p++;
  2680.         }
  2681.     }
  2682.   else
  2683.     if (image->compression == RunlengthEncodedCompression)
  2684.       {
  2685.         if (image->colors <= 256)
  2686.           for (i=0; i < image->packets; i++)
  2687.           {
  2688.             *q++=(unsigned char) p->index;
  2689.             *q++=p->length;
  2690.             count+=(p->length+1);
  2691.             p++;
  2692.           }
  2693.         else
  2694.           for (i=0; i < image->packets; i++)
  2695.           {
  2696.             *q++=(unsigned char) (p->index >> 8);
  2697.             *q++=(unsigned char) p->index;
  2698.             *q++=p->length;
  2699.             count+=(p->length+1);
  2700.             p++;
  2701.           }
  2702.       }
  2703.     else
  2704.       if (image->colors <= 256)
  2705.         for (i=0; i < image->packets; i++)
  2706.         {
  2707.           for (j=0; j <= ((int) p->length); j++)
  2708.             *q++=(unsigned char) p->index;
  2709.           count+=(p->length+1);
  2710.           p++;
  2711.         }
  2712.       else
  2713.         {
  2714.           register unsigned char
  2715.             xff00,
  2716.             xff;
  2717.  
  2718.           for (i=0; i < image->packets; i++)
  2719.           {
  2720.             xff00=(unsigned char) (p->index >> 8);
  2721.             xff=(unsigned char) p->index;
  2722.             for (j=0; j <= ((int) p->length); j++)
  2723.             {
  2724.               *q++=xff00;
  2725.               *q++=xff;
  2726.             }
  2727.             count+=(p->length+1);
  2728.             p++;
  2729.           }
  2730.         }
  2731.   image->packets=packets;
  2732.   /*
  2733.     Guarentee the correct number of pixel packets.
  2734.   */
  2735.   if (count < (image->columns*image->rows))
  2736.     {
  2737.       Warning("insufficient image data in",image->filename);
  2738.       return(False);
  2739.     }
  2740.   else
  2741.     if (count > (image->columns*image->rows))
  2742.       {
  2743.         Warning("too much image data in",image->filename);
  2744.         return(False);
  2745.       }
  2746.   return(True);
  2747. }
  2748.  
  2749. /*
  2750. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2751. %                                                                             %
  2752. %                                                                             %
  2753. %                                                                             %
  2754. %   S U N D e c o d e I m a g e                                               %
  2755. %                                                                             %
  2756. %                                                                             %
  2757. %                                                                             %
  2758. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2759. %
  2760. %  Function SUNDecodeImage unpacks the packed image pixels into
  2761. %  runlength-encoded pixel packets.
  2762. %
  2763. %  The format of the SUNDecodeImage routine is:
  2764. %
  2765. %      status=SUNDecodeImage(compressed_pixels,pixels,number_columns,
  2766. %        number_rows)
  2767. %
  2768. %  A description of each parameter follows:
  2769. %
  2770. %    o status:  Function SUNDecodeImage returns True if all the pixels are
  2771. %      uncompressed without error, otherwise False.
  2772. %
  2773. %    o compressed_pixels:  The address of a byte (8 bits) array of compressed
  2774. %      pixel data.
  2775. %
  2776. %    o pixels:  The address of a byte (8 bits) array of pixel data created by
  2777. %      the uncompression process.  The number of bytes in this array
  2778. %      must be at least equal to the number columns times the number of rows
  2779. %      of the source pixels.
  2780. %
  2781. %    o number_columns:  An integer value that is the number of columns or
  2782. %      width in pixels of your source image.
  2783. %
  2784. %    o number_rows:  An integer value that is the number of rows or
  2785. %      heigth in pixels of your source image.
  2786. %
  2787. %
  2788. */
  2789. unsigned int SUNDecodeImage(compressed_pixels,pixels,number_columns,number_rows)
  2790. unsigned char
  2791.   *compressed_pixels,
  2792.   *pixels;
  2793.  
  2794. unsigned int
  2795.   number_columns,
  2796.   number_rows;
  2797. {
  2798.   register int
  2799.     count;
  2800.  
  2801.   register unsigned char
  2802.     *p,
  2803.     *q;
  2804.  
  2805.   unsigned char
  2806.     byte;
  2807.  
  2808.   p=compressed_pixels;
  2809.   q=pixels;
  2810.   while ((q-pixels) <= (number_columns*number_rows))
  2811.   {
  2812.     byte=(*p++);
  2813.     if (byte != 128)
  2814.       *q++=byte;
  2815.     else
  2816.       {
  2817.         /*
  2818.           Runlength-encoded packet: <count><byte>
  2819.         */
  2820.         count=(*p++);
  2821.         if (count > 0)
  2822.           byte=(*p++);
  2823.         while (count >= 0)
  2824.         {
  2825.           *q++=byte;
  2826.           count--;
  2827.         }
  2828.      }
  2829.   }
  2830.   return(True);
  2831. }
  2832.